package com.example.block_chain.service.impl.account;

import com.example.block_chain.common.SystemParameters;
import com.example.block_chain.domain.pojo.*;
import com.example.block_chain.domain.pojo.Class;
import com.example.block_chain.domain.vo.LoginVo;
import com.example.block_chain.service.classs.AdminClassService;
import com.example.block_chain.service.classs.ClassService;
import com.example.block_chain.service.faculty.FacultyService;
import com.example.block_chain.service.major.MajorService;
import com.example.block_chain.utils.Admin.AdminDetailsImpl;
import com.example.block_chain.utils.JwtUtil;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.service.account.LoginService;
import com.example.block_chain.utils.KeyValueReaderUtil;
import com.example.block_chain.utils.student.StudentDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Resource
    private AdminClassService adminClassService;

    @Resource
    private ClassService classService;

    @Resource
    private MajorService majorService;

    @Resource
    private FacultyService facultyService;

    @Override
    public ResponseResult getToken(String username, String password, HttpSession session) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);


        Authentication authenticate = authenticationManager.authenticate(authenticationToken);

        //如果登录失败，会自动处理

        StudentDetailsImpl loginStudent = (StudentDetailsImpl) authenticate.getPrincipal();
        Student student = loginStudent.getStudent();

        session.setAttribute("student", student);
        //student.getId()就是jwt_token中的user_id,这个没有用sno生成，用的student表中的主键生成
        String jwt = JwtUtil.createJWT("student" + student.getId().toString());

        Map<String, Object> map = new HashMap<>();
        //获取班级信息
        Class classInfo = classService.getClassInfoByCode(student);
        map.put(classInfo.getClassCode(), classInfo);
        //获取专业信息
        Major major = majorService.getMajor(student.getMajorCode());
        map.put(major.getMajorCode(), major);
        //获取学院信息
        Faculty faculty = facultyService.getFaculty(student.getFacultyCode());
        map.put(faculty.getFacultyCode(), faculty);
        //获取学校信息
        Map<String, Object> school = new HashMap<>();
        String name = KeyValueReaderUtil.getDigestByCode(student.getSchoolCode(), SystemParameters.SCHOOL_FILE_PATH);
        school.put("name", name);
        map.put(student.getSchoolCode(), school);
        return ResponseResult.okResult(new LoginVo(jwt, student, classInfo, map));
    }

    @Override
    public ResponseResult adminGetToken(String username, String password, HttpSession session) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);


        Authentication authenticate = authenticationManager.authenticate(authenticationToken);

        //如果登录失败，会自动处理

        AdminDetailsImpl loginAdmin = (AdminDetailsImpl) authenticate.getPrincipal();
        Admin admin = loginAdmin.getAdmin();


        session.setAttribute("admin", admin);
        //student.getId()就是jwt_token中的user_id,这个没有用sno生成，用的student表中的主键生成
        String jwt = JwtUtil.createJWT("admin" + admin.getId().toString());

        //如果不是超级管理员，返回个人信息
        String authority = admin.getAuthority();
        Map<String, Object> map = new HashMap<>();
        if (!SystemParameters.AUTHORITY_0.toString().equals(authority)) {
            //如果是院级管理员、辅导员，返回学院信息
            if (SystemParameters.AUTHORITY_2.toString().equals(authority) || SystemParameters.AUTHORITY_1.equals(authority)) {
                map.put(admin.getFacultyCode(), facultyService.getFaculty(admin.getFacultyCode()));
            }
            //获取学校信息
            Map<String, Object> school = new HashMap<>();
            String name = KeyValueReaderUtil.getDigestByCode(admin.getSchoolCode(), SystemParameters.SCHOOL_FILE_PATH);
            school.put("name", name);
            map.put(admin.getSchoolCode(), school);

            //如果是辅导员，查询管理员所带班级
            if (Objects.equals(authority, SystemParameters.AUTHORITY_1)) {
                List<String> classIdList = adminClassService.getClassList(admin.getId());
                List<Class> classList = classService.getClassInfoByIds(classIdList);
                session.setAttribute("admin" + admin.getId(), classList.stream().map(Class::getClassCode).collect(Collectors.toList()));
                return ResponseResult.okResult(new LoginVo(jwt, admin, classList, map));
            }
        }


        return ResponseResult.okResult(new LoginVo(jwt, admin, null, map));
    }
}
